In [33]:
number_list = [1, 2, 4, 8, 16, 32]
the_pythons = ["Graham", "Terry", "Michael", "Eric", "Terry", "John"]
mixed = [1, "Terry", 4]
print (mixed)
In [34]:
monty = ("Graham", "Terry", "Michael", "Eric", "Terry", "John")
In [35]:
# the entire tuple
print (monty)
In [36]:
# one element at a time
for name in monty:
print(name)
In [39]:
# indexing
print(the_pythons[2])
print(monty[2])
[] brackets or square brackets () parentheses {} curly braces or braces
In [7]:
# monty is the tuple, and the_pythons is a list
print (monty)
print (list(monty))
In [9]:
print (the_pythons)
print (tuple(the_pythons))
In [ ]:
# Safer than constants, because it is enforced by interpreter
CONVERSION_CONSTANT = 5/9
In [20]:
my_dict = {}
my_dict[3.14] = "pi"
my_dict["pi"] = 3.14159
my_dict[(1,2)] = "x,y coordinates"
my_dict[(2,3)] = "x,y coordinates"
print my_dict
In [40]:
my_dict[(1,2)] = [4, 5, 6, 7]
print my_dict
In [41]:
len(my_dict)
Out[41]:
In [14]:
phone_book = {"Graham":"555-111",
"Terry": "555-2222",
"Michael": "555-3333"}
In [15]:
phone_book
Out[15]:
In [18]:
phone_book['Michael']
Out[18]:
In [42]:
my_dict[(1,2)]
Out[42]:
In [43]:
phone_book['Wanda']
In [45]:
# Using 'in'
if "Michael" in phone_book:
print phone_book["Michael"]
In [46]:
# Using 'not in'
if "Wanda" not in phone_book:
print("Fish don't need phone numbers")
In [49]:
print(phone_book)
# Eric, Terry, John
phone_book["Eric"] = "555-4444"
phone_book["Terry"] = "555-5555"
phone_book["John"] = "555-6666"
In [50]:
del phone_book["John"]
In [51]:
print(phone_book)
In [53]:
if 'Michael' in phone_book:
del phone_book['Michael']
print(phone_book)
In [54]:
if '555-4444' in phone_book:
print ("Can match values too!")
In [55]:
for name in phone_book:
print (name, phone_book[name])
In [56]:
phone_book.items()
Out[56]:
In [57]:
phone_book.keys()
Out[57]:
In [58]:
phone_book.values()
Out[58]:
In [59]:
if '555-4444' in phone_book.values():
print("We can match values too")
In [62]:
even = False
if even = True:
print("It is even!")
In [63]:
154 >= 300 != False
Out[63]:
In [71]:
def is_equal(t1, t2):
# return t1 == t2
return t1.sort() == t2.sort()
list1 = ["name", "age", "temp"]
list2 = ["name", "temp", "age"]
if is_equal(list1, list2):
print("Same!")
else:
print ("Different!")